package com.love.marriage.userMembershipRewardInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.common.enums.MembershipType;
import com.love.marriage.membershipConfiguration.vo.MembershipConfigurationVo;
import com.love.marriage.userMembershipRewardInfo.entity.UserMembershipRewardInfo;
import com.love.marriage.userMembershipRewardInfo.mapper.UserMembershipRewardInfoMapper;
import com.love.marriage.userMembershipRewardInfo.service.UserMembershipRewardInfoService;
import com.love.marriage.userMembershipRewardInfo.vo.UserMembershipRewardInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  用户会员悬赏表服务实现类
 * author:       alex
 * createDate:   2025-01-25 11:20:43
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class UserMembershipRewardInfoServiceImp extends ServiceImpl<UserMembershipRewardInfoMapper, UserMembershipRewardInfo> implements UserMembershipRewardInfoService {

    private final UserMembershipRewardInfoMapper userMembershipRewardInfoMapper;

    @Override
    public Page<UserMembershipRewardInfoVo> getPage(Long pageNum, Long pageSize, UserMembershipRewardInfoVo userMembershipRewardInfoVo) {
        Page<UserMembershipRewardInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userMembershipRewardInfoMapper.getPage(page, userMembershipRewardInfoVo);
    }

    @Override
    public UserMembershipRewardInfoVo queryUserMembershipRewardInfo(Long id) {
        return userMembershipRewardInfoMapper.queryUserMembershipRewardInfo(id);
    }

    @Override
    public UserMembershipRewardInfoVo queryUserMembershipRewardInfo(UserMembershipRewardInfo userMembershipRewardInfo) {
        return userMembershipRewardInfoMapper.queryUserMembershipRewardInfoVo(userMembershipRewardInfo);
    }

    @Override
    public UserMembershipRewardInfoVo addUserMembershipRewardInfo(UserMembershipRewardInfoVo userMembershipRewardInfoVo) {
        UserMembershipRewardInfo userMembershipRewardInfo = new UserMembershipRewardInfo();
        BeanUtils.copyProperties(userMembershipRewardInfoVo, userMembershipRewardInfo);
        userMembershipRewardInfoMapper.insert(userMembershipRewardInfo);
        UserMembershipRewardInfoVo res = new UserMembershipRewardInfoVo();
        BeanUtils.copyProperties(userMembershipRewardInfo, res);
        return res;
    }

    @Override
    public UserMembershipRewardInfoVo updateUserMembershipRewardInfo(UserMembershipRewardInfoVo userMembershipRewardInfoVo) {
        UserMembershipRewardInfo userMembershipRewardInfo = new UserMembershipRewardInfo();
        BeanUtils.copyProperties(userMembershipRewardInfoVo, userMembershipRewardInfo);
        userMembershipRewardInfoMapper.updateById(userMembershipRewardInfo);
        return userMembershipRewardInfoVo;
    }

    @Override
    public Boolean deleteUserMembershipRewardInfo(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userMembershipRewardInfoMapper.deleteBatchIds(idArr);
        return true;
    }

    @Override
    public Boolean userMembershipRewardInfo(MembershipConfigurationVo membershipConfigurationVo,
                                            Long userId, Long businessId) {
        UserMembershipRewardInfo userMembershipRewardInfo = new UserMembershipRewardInfo();
        userMembershipRewardInfo.setUserId(userId);
        userMembershipRewardInfo.setMembershipId(membershipConfigurationVo.getId());
        UserMembershipRewardInfoVo userMembershipRewardInfoVo = queryUserMembershipRewardInfo(userMembershipRewardInfo);
        MembershipType membershipType = MembershipType.getMembershipTypeByCode(membershipConfigurationVo.getMembership());
        LocalDate startTime = LocalDate.now();
        if (userMembershipRewardInfoVo == null) {
            UserMembershipRewardInfoVo newRewardInfo = new UserMembershipRewardInfoVo();
            newRewardInfo.setMembershipId(businessId);
            newRewardInfo.setUserId(userId);
            // 获取会员时长
            LocalDate endTime = membershipType.calculateStartTime(startTime);
            newRewardInfo.setStartTime(startTime.atStartOfDay());
            newRewardInfo.setEndTime(endTime.minusDays(1).atStartOfDay());
            addUserMembershipRewardInfo(newRewardInfo);
        } else {
            // 如果是会员，当前时间和会员开始时间、结束时间比较
            if (LocalDate.now().atStartOfDay().isBefore(userMembershipRewardInfoVo.getEndTime())) {
                //如果当前时间在结束时间之前，直接在结束时间 + 会员对应的时长
                LocalDate endTime = membershipType.calculateStartTime(userMembershipRewardInfoVo.getEndTime().toLocalDate());
                userMembershipRewardInfoVo.setEndTime(endTime.atStartOfDay());
            } else {
                // 如果当前时间在结束时间之后，直接修改开始结束时间
                // 获取会员时长
                LocalDate endTime = membershipType.calculateStartTime(startTime);
                userMembershipRewardInfoVo.setStartTime(startTime.atStartOfDay());
                userMembershipRewardInfoVo.setEndTime(endTime.minusDays(1).atStartOfDay());
            }
            updateUserMembershipRewardInfo(userMembershipRewardInfoVo);
        }
        return true;
    }
}
